<?php
// +----------------------------------------------------------------------
// |店铺管理
// +----------------------------------------------------------------------
// |Data:2016-06-24 下午3:31
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Common\Controller\AdminCommonController;

class ShopController extends AdminCommonController{
    private $_model;
    protected function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->_model = D('Common/Shop');
    }

    public function index(){
        $map = array();
        if($_SESSION['administrator']){
           $map['id'] = array('gt',0);
        }else{
            $map['uid'] = $this->uid;
        }
        $list = $this->_model->getList($map,'add_time desc');
        foreach($list['data'] as &$val){
            $val['area'] = D('Common/Area')->getFullNameByAreaCode($val['area_code'],3,'-').$val['address'];
            $val['cate'] = D('Admin/ShopCate')->getField($val['cate_id']);
            list($s_time,$e_time) = explode(',',$val['sales_time'],2);
            $val['s_time'] = $s_time;
            $val['e_time'] = $e_time;
        }
        $this->assign($list);
        $this->display();
    }

    /**
     * 编辑
     */
    public function edit(){
        $id = I('get.id');
        if($id){
            $detail = $this->_model->detail($id);
            $detail['dpriceArr'] = unserialize($detail['deviler_price']);
            $detail['fpriceArr'] = unserialize($detail['free_price']);
            $this->assign('detail',$detail);
        }
        $flag = 2;
        if($_SESSION['administrator']){
            $map = array();
            $map['id'] = array('neq',C('MANAGE_id'));
            $userList = D('Admin/Manage')->getManageList($map);
            $this->assign('userList',$userList);
            $flag = 1;
        }
        $list = D('Admin/ShopCate')->getAll();
        $this->assign('list',$list);
        $this->assign('flag',$flag);
        $this->display();
    }

    /**
     * 数据更新
     */
    public function update(){
        if(IS_POST){
            $data = $this->checkPostData();
            empty($data) || !is_array($data) && $this->returnData('请填写表单数据！');
            $msg = $data['id'] ? '编辑' : '添加';
            $result = $this->_model->update($data);
            if($result){
                $this->addHistoryLog($msg.'店铺成功');
                $this->returnData($msg.'店铺成功！',1,'/Shop/index');
            }else{
                $this->returnData($msg.'店铺失败！');
            }
        }else{
            $this->returnData('请求方式错误！');
        }
    }

    /**
     * 删除
     */
    public function del(){
        $id = I('get.id');
        empty($id) && $this->returnData('请选择操作项！');
        $isHas = $this->_model->hasProject($id);
        !empty($isHas) && $this->returnData('该店铺下已存在商品，不能删除！');
        $result = $this->_model->del($id,$isHas);
        if($result){
            $this->addHistoryLog('删除店铺成功');
            $this->returnData('删除成功！',1);
        }else{
            $this->returnData('删除失败！');
        }
    }

    /**
     * 更改状态
     */
    public function setStatus(){
        $id = I('get.id');
        empty($id) && $this->returnData('请选择操作项！');
        $detail = $this->_model->detail($id);
        empty($detail) && $this->returnData('该店铺不存在!');
        $status = $detail['status'] == 1 ? 2 : 1;
        $data = array(
            'status'=>$status,
            'id'=>$id,
            'uid'=>$detail['uid']
        );
        $result = $this->_model->update($data);
        if($result){
            $msg = $status == 1 ? '营业' : '停业';
            $this->addHistoryLog('设置店铺'.$msg.'成功');
            $this->returnData('设置成功！',1);
        }else{
            $this->returnData('设置失败！');
        }
    }

    /**
     * 检测表单数据
     * @return array
     */
    private function checkPostData(){
        $id = I('post.id');
        $uid = I('post.uid');
        $title = I('post.title');
        $cid = I('post.cid');
        $tel = I('post.tel');
        $bus_stime = I('post.bus_stime');
        $bus_etime = I('post.bus_etime');
        $location = I('post.location');
        $area_path = I('post.area');
        $address = I('post.address');
        $smin_km = I('post.smin_km');
        $smax_km = I('post.smax_km');
        $sprice = I('post.sprice');
        $is_free = I('post.is_free');
        $dmin_km = I('post.dmin_km');
        $dmax_km = I('post.dmax_km');
        $dprice = I('post.dprice');
        $der_stime = I('post.der_stime');
        $der_etime = I('post.der_etime');
        $pub_num = I('post.pub_num');
        $reservation = I('post.reservation',1);
        $status = I('post.status',1);
        $desc = $_POST['desc'];
        $pic = I('post.pic');

        if($_SESSION['administrator']){
            if(empty($uid)){
                $this->returnData('请先选择店铺人员！');
            }
        }else{
            $uid = $this->manage_id;
        }
        empty($title) && $this->returnData('请输入店铺名称！');
        if(abslength($title) > 100) $this->returnData('店铺名称1~100个字符！');
        empty($cid) && $this->returnData('请选择店铺类型！');
        empty($tel) && $this->returnData('请输入电话号码！');
        if(!telRegx($tel)) $this->returnData('电话号码格式错误！');
        empty($bus_stime) && $this->returnData('请输入营业开始时间！');
        empty($bus_etime) && $this->returnData('请输入营业结束时间！');
        if(empty($location) || empty($area_path) || empty($address)) $this->returnData('请设置店铺地点！');
        if(empty($smin_km) || empty($smax_km) || empty($sprice)) $this->returnData('请设置起送价！');
        foreach($smin_km as $val){
            if(!checkRegx("/^[0-9]*/",$val)){
                $this->returnData('起送价路程格式错误！');
            }
        }
        foreach($smax_km as $val){
            if(!checkRegx("/^[0-9]*/",$val)){
                $this->returnData('起送价路程格式错误！');
            }
        }
        foreach($sprice as $val){
            if(!checkRegx("/^[0-9]*/",$val)){
                $this->returnData('起送价程格式错误！');
            }
        }
        empty($is_free) && $this->returnData('请选择外送费');
        if($is_free == 2){
            if(empty($dmin_km) || empty($dmax_km) || empty($dprice)) $this->returnData('请输入外送费！');
            foreach($dmin_km as $val){
                if(!checkRegx("/^[0-9]*/",$val)){
                    $this->returnData('外送费路程格式错误！');
                }
            }
            foreach($dmax_km as $val){
                if(!checkRegx("/^[0-9]*/",$val)){
                    $this->returnData('外送费路程格式错误！');
                }
            }
            foreach($dprice as $val){
                if(!checkRegx("/^[0-9]*/",$val)){
                    $this->returnData('外送费程格式错误！');
                }
            }
        }
        empty($der_stime) && $this->returnData('请输入配送开始时间！');
        empty($der_etime) && $this->returnData('请输入配送结束时间！');
        empty($pub_num) && $this->returnData('请输入公众号！');
        empty($desc) && $this->returnData('请输入分店描述！');
        //if(abslength($desc) > 200) $this->returnData('描述字数为1~200个字符！');
        empty($pic) && $this->returnData('请上传店铺图片！');
        $pic = is_array($pic) ? arr2str($pic) : $pic;

        $temp = array(
            'smin_km'=>$smin_km,
            'smax_km'=>$smax_km,
            'sprice'=>$sprice,
        );
        asort($sprice);
        $min_send_price = $sprice[0];
        $deviler_price = serialize($temp);
        if($is_free == 2){
            $arr = array(
                'dmin_km'=>$dmin_km,
                'dmax_km'=>$dmax_km,
                'dprice'=>$dprice
            );
            $free_price = serialize($arr);
        }else{
            $free_price = '';
        }

        $data = array(
            'id'=>$id,
            'uid'=>$uid,
            'title'=>$title,
            'pic'=>$pic,
            'cate_id'=>$cid,
            'sales_time'=>$bus_stime.','.$bus_etime,
            'area_code'=>$area_path,
            'address'=>$address,
            'tel'=>$tel,
            'deviler_price'=>$deviler_price,
            'free_price'=>$free_price,
            'deviler_time'=>$der_stime.','.$der_etime,
            'pub_num'=>$pub_num,
            'desc'=>$desc,
            'status'=>$status,
            'min_send_price'=>$min_send_price,
            'is_reservation'=>$reservation,
        );

        if($id){
            $detail = $this->_model->detail();
            if($detail['address'] != $address){
                $area = D('Admin/Area')->getFullNameByAreaCode($area_path,3,'');
                $allAddress = $area.$address;
                $locationArr = getLocationByAddress($allAddress);
                $data['location'] = $locationArr['lng'].','.$locationArr['lat'];
            }
        }else{
            $area = D('Admin/Area')->getFullNameByAreaCode($area_path,3,'');
            $allAddress = $area.$address;
            $locationArr = getLocationByAddress($allAddress);
            $data['location'] = $locationArr['lng'].','.$locationArr['lat'];
        }

        if($id){
            $data['edit_time'] = time();
            $data['id'] = $id;
        }else{
            $data['add_time'] = time();
        }
        return $data;
    }
}
?>